package com.dcy.daily.common.utils.mybatisGen;

import com.dcy.daily.common.utils.DateUtil;
import com.dcy.daily.common.utils.StringUtils;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

public class MybatisGenerator extends DefaultCommentGenerator {
    private Properties properties;

    public MybatisGenerator() {
        properties = System.getProperties();
    }
    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        // 添加字段注释
        StringBuffer sb = new StringBuffer();
        field.addJavaDocLine("/**");
        if (StringUtils.isNotEmpty(introspectedColumn.getRemarks())) {
            field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
        }
        sb.append(" * 表字段 : ");
        sb.append(introspectedColumn.getActualColumnName());
        field.addJavaDocLine(sb.toString());
        field.addJavaDocLine(" */");
    }
    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {

    }
    @Override
    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {

    }
    @Override
    public void addGetterComment(Method method, IntrospectedTable introspectedTable,
                                 IntrospectedColumn introspectedColumn) {

    }
    @Override
    public void addSetterComment(Method method, IntrospectedTable introspectedTable,
                                 IntrospectedColumn introspectedColumn) {

    }
    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {

    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        String remarks = introspectedTable.getRemarks();
        String tableName = introspectedTable.getFullyQualifiedTable().toString();
        topLevelClass.addJavaDocLine("/**");
        topLevelClass.addJavaDocLine(" * @description " + tableName + " " + remarks);
        topLevelClass.addJavaDocLine(" * @date " + DateUtil.formatDateToString(new Date(),DateUtil.FORMAT_YYYY_MM_DD_HH_MM));
        topLevelClass.addJavaDocLine(" */");
    }


    /**
     *
     * @param webName 配置文件所在模块名；单模块工程，参数为空
     */
    public static void start(String webName){
        try{
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            File file = new File((StringUtils.isEmpty(webName)?"":webName) + "src/main/resources/mybatis-generate.xml");
            InputStream is = new FileInputStream(file);
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(is);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
            for(String warning:warnings){
                System.out.println(warning);
            }
            System.out.println(System.getProperty("user.dir"));
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        start("dcyDaily-skill/");
    }
}